// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
// 引入element UI
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import App from "./App";

// 引入路由
import router from "./router";

// 引入状态管理
import store from "./vuex/store";

// 引入icon
import "./assets/icon/iconfont.css";

import VueQuillEditor from "vue-quill-editor";

Vue.use(VueQuillEditor);

import "quill/dist/quill.core.css";

import "quill/dist/quill.snow.css";

import JsonExcel from "vue-json-excel";
Vue.component("downloadExcel", JsonExcel);

// 引入echarts
import echarts from "echarts";
Vue.prototype.$echarts = echarts;

import axios from "axios";
Vue.prototype.$axios = axios;

Vue.config.productionTip = false;

// 使用element UI
Vue.use(ElementUI);

// 过滤器
import * as custom from "./utils/util";

let erCodeUrl = 'https://h5.gwyckj.com/#/pages/tabbar/index/attachment/detail/detail?electricId='

Vue.prototype.$erCodeUrl = erCodeUrl;

let carouselUrl = "/pages/shopping_mall/goods_details?id="

Vue.prototype.$carouselUrl = carouselUrl;

Object.keys(custom).forEach(key => {
  Vue.filter(key, custom[key]);
});


// 路由拦截器
router.beforeEach((to, from, next) => {
  if (to.matched.length != 0) {
    if (to.meta.requireAuth) {
      // 判断该路由是否需要登录权限
      if (Boolean(localStorage.getItem("userInfo"))) {
        // 通过vuex state获取当前的user是否存在
        next();
      } else {
        next({
          path: "/login",
          query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
        });
      }
    } else {
      if (Boolean(localStorage.getItem("userInfo"))) {
        // 判断是否登录
        if (to.path != "/" && to.path != "/login") {
          //判断是否要跳到登录界面
          next();
        } else {
          /**
           * 防刷新，如果登录，修改路由跳转到登录页面，修改路由为登录后的首页
           *
           */
          var userdata = JSON.parse(localStorage.getItem("userdata"));
          if (userdata.rolesId == "1") {
            next({
              path: "/index/Index"
            });
          } else {
            next({
              path: "/index/Index"
            });
          }
        }
      } else {
        next();
      }
    }
  } else {
    next({
      path: "/login",
      query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
    });
  }
});


/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  store, //使用store vuex状态管理
  components: { App },
  template: "<App/>",
  data: {
    // 空的实例放到根组件下，所有的子组件都能调用
    Bus: new Vue()
  }
});

//时间戳转换
Vue.prototype.transformDate = function (shijianchuo) {
  //截取字符串前10位
  var time = new Date(shijianchuo * 1000);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  return (
    y +
    "-" +
    this.add0(m) +
    "-" +
    this.add0(d) +
    " " +
    this.add0(h) +
    ":" +
    this.add0(mm) +
    ":" +
    this.add0(s)
  );
};

//时间戳转换yy-mm-dd
Vue.prototype.transformYymmddDate = function (shijianchuo) {
  if (shijianchuo != '' && shijianchuo != null) {
    //截取字符串前10位
    var time = new Date(shijianchuo * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return (
      y +
      "-" +
      this.add0(m) +
      "-" +
      this.add0(d)
    );
  } else {
    return ''
  }
};
//时间戳转换
Vue.prototype.add0 = function (m) {
  return m < 10 ? "0" + m : m;
};

// 滚动条
import horizontalScroll from 'el-table-horizontal-scroll'
// 全局使用滚动条
Vue.use(horizontalScroll)